package com.cskaoyan.service;

import com.cskaoyan.bean.baseresultVO.WxResultListVo;
import com.cskaoyan.bean.domain.Comment;
import com.cskaoyan.bean.domain.CommentExample;
import com.cskaoyan.bean.domain.User;
import com.cskaoyan.bean.domain.UserExample;
import com.cskaoyan.bean.wxComment.CommentListVo;
import com.cskaoyan.bean.wxComment.CommentVo;
import com.cskaoyan.bean.wxComment.UserInfo;
import com.cskaoyan.bean.wxComment.WxCountList;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * @Author lirui
 * @Date 2021/12/2 17:08
 * @describe:
 */

@Service
public class WxCommentServiceImpl implements WxCommentService {

    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public WxResultListVo queryCommentList(Integer valueId,Byte type,Integer limit,Integer page,Integer showType) {

        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        ArrayList<CommentListVo> listVoArrayList = new ArrayList<>();

        UserInfo userInfo = new UserInfo();
        for(Comment comment:comments){
            CommentListVo commentListVo = new CommentListVo();
            commentListVo.setAddTime(comment.getAddTime());
            commentListVo.setAdminContent(comment.getAdminContent());
            commentListVo.setContent(comment.getContent());
            commentListVo.setPicList(comment.getPicUrls());
             Integer userId=comment.getUserId();
            UserExample example = new UserExample();
            UserExample.Criteria criteria1 = example.createCriteria();
            criteria1.andIdEqualTo(userId);
            List<User> users = userMapper.selectByExample(example);

            userInfo.setAvatarUrl(users.get(0).getAvatar());
            userInfo.setNickName(users.get(0).getNickname());
            commentListVo.setUserInfo(userInfo);
            listVoArrayList.add(commentListVo);

        }

        PageInfo info = new PageInfo();
        Integer total=(int)info.getTotal();
        Integer pages=info.getPages();
        WxResultListVo wxResultListVo = new WxResultListVo();




        wxResultListVo.setList(listVoArrayList);
        wxResultListVo.setTotal(total);
        wxResultListVo.setPages(pages);
        wxResultListVo.setLimit(limit);
        wxResultListVo.setPage(page);


        return wxResultListVo;
    }

   @Override
    public WxCountList queryCountList(Integer valueId, Integer type) {
       CommentExample commentExample = new CommentExample();
       CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        Byte typeChange=(byte)type.intValue();
        criteria.andTypeEqualTo(typeChange);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        Integer allCount=comments.size();
        ArrayList<String[]> picList = new ArrayList<>();

       for (Comment comment:comments){
           String[] picUrls = comment.getPicUrls();
           if (picList!=null) {
            picList.add(picUrls);
           }
       }

        Integer hasPicCount = picList.size();
        WxCountList wxCountList = new WxCountList(allCount, hasPicCount);
       return wxCountList;

    }

    @Override
    public Comment addCommentPost(Comment commentVo) {
        Comment commentVo1 = new Comment();

        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()){
            User user = (User) subject.getPrincipals().getPrimaryPrincipal();
            commentVo1.setValueId(commentVo.getValueId());
            commentVo1.setType(commentVo.getType());
            commentVo1.setContent(commentVo.getContent());
            commentVo1.setUserId(user.getId());
            commentVo1.setHasPicture(commentVo.getHasPicture());
            commentVo1.setPicUrls(commentVo.getPicUrls());
            commentVo1.setStar(commentVo.getStar());
            commentVo1.setAddTime(new Date());
            commentVo1.setUpdateTime(new Date());

            commentMapper.insertSelective(commentVo1);


        }
        return commentVo1;
    }

        


}
